﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_GroupPages
{
    public partial class Edit : AdminPage
    {
        private GroupPages curGroupPages = new GroupPages();
        private string codeno = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            codeno = (Request.QueryString["codeno"]) == null ? "" : Request.QueryString["codeno"].Trim();
            if (!IsPostBack)
            {
                //上次请求路径
                if (hidReferrer.Value.Trim() == "")
                {
                    Uri uri = Request.UrlReferrer;
                    if (uri != null)
                    {
                        hidReferrer.Value = uri.ToString();
                    }
                }
                InitData();
            }
        }

        private void InitData()
        {
            GroupPagesInfo model = curGroupPages.GetGroupPagesInfo(codeno);
            if (model != null)
            {
                txtPageName.Text = model.pagename;
                txtPagePath.Text = model.pagepath;
                txtGroupName.Text = model.groupname;
            }
        }


        protected void btnEdit_Click(object sender, EventArgs e)
        {
            GroupPagesInfo model = new GroupPagesInfo();
            string tmp_codeno = hidPageCode.Value.Trim();
            if (tmp_codeno.StartsWith(HtmlLabel.powerCode) && tmp_codeno.EndsWith(HtmlLabel.powerCode))
            {
                tmp_codeno = tmp_codeno.Replace(HtmlLabel.powerCode, "");
            }
            else
            {
                tmp_codeno = "";
            }

            model.codeno = (tmp_codeno == "") ? codeno : tmp_codeno;
            model.pagename = txtPageName.Text.Trim();
            model.pagepath = txtPagePath.Text.Trim();
            model.groupname = txtGroupName.Text.Trim();


            if (curGroupPages.Update(codeno, model))
            {
                string refurl = hidReferrer.Value.Trim();
                if (refurl == "" || refurl == this.Request.Path.Trim())
                {
                    Response.Redirect("../Message.aspx?message=修改授权页成功！" + "&z=yes");
                }
                else
                {
                    Response.Redirect(refurl);
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改授权页失败，授权代码不存在！&z=no");
            }

        }
    }
}
